﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraEditors.DXErrorProvider;
using System.Windows.Forms;
namespace NNLInterface
{
    class DataValid
    {

        /// <summary>
        /// 设定控件的值不能为空白
        /// </summary>
        /// <param name="dXValidationProvider">验证对象提供者</param>
        /// <param name="ctl">控件对象</param>
        /// <param name="messageId">出错信息，可以用ID，可以用字符串</param>
        public static void SetControlValidBlank(DXValidationProvider dXValidationProvider, Control ctl, string messageId)
        {
            ConditionValidationRule notEmptyValidationRule = new ConditionValidationRule();
            notEmptyValidationRule.ConditionOperator = ConditionOperator.IsNotBlank;
            notEmptyValidationRule.ErrorText = messageId;
            dXValidationProvider.SetValidationRule(ctl, notEmptyValidationRule);

            //switch (ctl.GetType().ToString().Trim())
            //{
            //    case "DevExpress.XtraEditors.ButtonEdit":

            //        break;
            //}
        }


        /// <summary>
        /// 验证控件的值介一个区间内
        /// </summary>
        /// <param name="dXValidationProvider">验证对象提供者</param>
        /// <param name="ctl">控件对象</param>
        /// <param name="messageId">出错信息，可以用ID，可以用字符串</param>
        /// <param name="value1">起始值</param>
        /// <param name="value2">结束值</param>
        public static void SetControlValidBetween(DXValidationProvider dXValidationProvider, Control ctl, string messageId,decimal value1,decimal value2)
        {
            ConditionValidationRule rangeValidationRule = new ConditionValidationRule();
            rangeValidationRule.ConditionOperator = ConditionOperator.Between ;
            rangeValidationRule.Value1 = value1;
            rangeValidationRule.Value2 = value2;
            rangeValidationRule.ErrorText = messageId;
            dXValidationProvider.SetValidationRule(ctl, rangeValidationRule);
        }


        /// <summary>
        /// 验证控件的值是否大于指定的值
        /// </summary>
        /// <param name="dXValidationProvider">验证对象提供者</param>
        /// <param name="ctl">控件对象</param>
        /// <param name="messageId">出错信息，可以用ID，可以用字符串</param>
        /// <param name="value1">指定的值</param>
        public static void SetControlValidGreater(DXValidationProvider dXValidationProvider, Control ctl, string messageId, decimal value1)
        {
            ConditionValidationRule rangeValidationRule = new ConditionValidationRule();
            rangeValidationRule.ConditionOperator = ConditionOperator.Greater ;
            rangeValidationRule.Value1 = value1;
            rangeValidationRule.Value2=value1;
            rangeValidationRule.ErrorText =  messageId;
            dXValidationProvider.SetValidationRule(ctl, rangeValidationRule);
        }

        /// <summary>
        /// 判断是否为数字
        /// </summary>
        /// <param name="ojb">字符串对象</param>
        /// <returns>返加是否为数字，真为数字，否为非数字</returns>
        public static bool IsNumeric(string  ojb)
        {
            int n;
            if (int.TryParse(ojb, out n))
            {
                return true;
            }
            else
            {
                return false;
            }

        }
    }
}
